package com.link.play.fit.ai.ui.guide.ac

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.databinding.AcGuideBinding
import com.link.play.fit.ai.ui.guide.GuideFg
import com.zhpan.indicator.enums.IndicatorSlideMode
import com.zhpan.indicator.enums.IndicatorStyle
import eightbitlab.com.blurview.BlurAlgorithm
import eightbitlab.com.blurview.RenderEffectBlur
import eightbitlab.com.blurview.RenderScriptBlur
import java.util.concurrent.atomic.AtomicBoolean

class GuideAc : BaseActivity<AcGuideBinding>(AcGuideBinding::inflate) {

    private val page1 by lazy {
        GuideFg().apply {
            pageIndex = 0
        }
    }
    private val page2 by lazy {
        GuideFg().apply {
            pageIndex = 1
        }
    }
    private val page3 by lazy {
        GuideFg().apply {
            pageIndex = 2
        }
    }
    private val fragments: MutableList<Fragment> by lazy {
        mutableListOf(page1, page2, page3)
    }


    override fun useDefaultImmersive() {
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom)
            insets
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        initViewPager2()
        setBlurView()
        binding.tvNext.setOnClickListener {
//            if (currentIndex < 2) {
//                val next = currentIndex + 1
//                binding.viewPager2.setCurrentItem(next, true)
//            } else {
            startActivity(Intent(this, GuidePbAc::class.java))
            finish()
//            }
        }
    }

    private var currentIndex = 0
    private fun initViewPager2() {
        binding.indicatorView.apply {

            setSliderColor(
                Color.parseColor("#FFD9DADC"),
                ContextCompat.getColor(this@GuideAc, R.color.colorPrimary)
            )
            setSliderWidth(resources.getDimension(R.dimen.dp_10))
            setSliderHeight(resources.getDimension(R.dimen.dp_10))
            setSlideMode(IndicatorSlideMode.NORMAL)
            setIndicatorStyle(IndicatorStyle.CIRCLE)
            setPageSize(fragments.size)
            notifyDataChanged()

        }
        binding.viewPager2.run {
            adapter = object : FragmentStateAdapter(this@GuideAc) {
                override fun getItemCount(): Int {
                    return fragments.size
                }

                override fun createFragment(position: Int): Fragment {
                    return fragments[position]
                }
            }
        }
        binding.viewPager2.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
                binding.indicatorView.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }

            override fun onPageSelected(position: Int) {
                changePage(position)
                binding.indicatorView.onPageSelected(position)
            }
        })
    }

    private fun changePage(position: Int) {
        currentIndex = position
        when (position) {
            0 -> {
                binding.ivScan.visibility = View.VISIBLE
                binding.ll.visibility = View.INVISIBLE
                binding.blurViewGoal.visibility = View.INVISIBLE
                binding.iv.setImageResource(R.mipmap.image)
            }

            1 -> {
                binding.ll.visibility = View.VISIBLE
                binding.ivScan.visibility = View.INVISIBLE
                binding.blurViewGoal.visibility = View.INVISIBLE
                binding.iv.setImageResource(R.mipmap.image)
                setBlurView()
            }

            2 -> {
                binding.blurViewGoal.visibility = View.VISIBLE
                binding.ll.visibility = View.INVISIBLE
                binding.ivScan.visibility = View.INVISIBLE
                binding.iv.setImageResource(R.mipmap.image2)
                setBlurView2()
            }
        }

    }

    private var setBlur = AtomicBoolean(false)
    private fun setBlurView() {
        if (setBlur.get()) return
        setBlur.set(true)

        val windowBackground = window.decorView.background

        arrayOf(binding.blurView1, binding.blurView2, binding.blurView3).forEach {
            it.setupWith(binding.fl, getBlurAlgorithm())
                .setFrameClearDrawable(windowBackground)
                .setOverlayColor(Color.TRANSPARENT)
                .setBlurAutoUpdate(true)
                .setBlurRadius(2f)

        }

    }

    private fun getBlurAlgorithm(): BlurAlgorithm {
        val algorithm = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            RenderEffectBlur()
        } else {
            RenderScriptBlur(this)
        }
        return algorithm
    }


    private var setBlur2 = AtomicBoolean(false)
    private fun setBlurView2() {
        if (setBlur2.get()) return
        setBlur2.set(true)
        val windowBackground = window.decorView.background
        binding.blurViewGoal.setupWith(binding.fl, getBlurAlgorithm())
            .setFrameClearDrawable(windowBackground)
            .setOverlayColor(Color.parseColor("#B51A1A1A"))
            .setBlurAutoUpdate(true)
            .setBlurRadius(2f)
    }
}